package com.apes.fn.scm.saleReport.service.process.orderProcess;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.crm.customerExpand.FnCustomerExpandRepository;
import com.apes.fn.scm.saleReport.service.process.CreateDetailsProcess;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.LogicalUtil;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarLabel;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.car.repository.CarConfigurationRepository;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.voucher.model.Voucher;
import com.apes.scm.voucher.model.VoucherExtension;
import com.apes.scm.voucher.model.VoucherItem;
import com.apes.scm.voucher.repository.VoucherExtensionRepository;
import com.apes.scm.voucher.repository.VoucherRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 业务凭证生成营业额明细与成本明细
 */
@Service
public class VoucherProcess extends CreateDetailsProcess {

    @Autowired
    private VoucherRepository voucherRepository;
    @Autowired
    private FnCustomerExpandRepository fnCustomerExpandRepository;
    @Autowired
    private CarConfigurationRepository carConfigurationRepository;
    @Autowired
    private VoucherExtensionRepository voucherExtensionRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        Voucher voucher = voucherRepository.findOne(id);
        voucher.getVoucherItems().forEach(voucherItem -> createDetails(MapUtil.mapper("order", voucherItem)));
    }

    /**
     * @param map {"order": VoucherItem}
     *
     */
    @Override
    public Map<String, Object> getModel(Map map) {
        VoucherItem voucherItem = (VoucherItem) map.get("order");
        Voucher voucher = voucherItem.getVoucher();
        Product product = voucherItem.getProduct();
        String customerExpand = voucher.getCustomerExpand();
        //是否生成营业额明细标记
        boolean sign = true;
        if (    //非本公司合作伙伴
                voucher.getPartner().getParty().getId() == voucher.getCompany().getParty().getId() ||
                        //不包含场景为RO002卖单定损，SAL001标准分销,SAL002赋能分销,SR001分销退货
                        voucher.getScene() != null && LogicalUtil.in(voucher.getScene().getId(),"RO002","SAL001","SAL002","SR001","SR005") ||
                        //凭证分类为06内协销售、INSURANCE卖单定损、CROSS_IC异业销售、CROSS_IC_RETURN异业退货不要
                        LogicalUtil.in(voucher.getClassification(),"06","INSURANCE","CROSS_IC","CROSS_IC_RETURN") ||
                        //不包含源单据为报损、报溢
                        "ProfitAndLoss".equals(voucher.getOriginBusinessType())
        ) sign = false;
        //厂牌
        CarLabel carLabel = null;
        //车型
        CarModel carModel = null;
        //车牌
        String carLicense;
        if(customerExpand != null){
            //车辆扩展信息
            FnCustomerExpand fnCustomerExpand = fnCustomerExpandRepository.findOne(customerExpand);
            carLicense = fnCustomerExpand.getCarLicense();
            String jekunModelCode = fnCustomerExpand.getJekunModelCode();
            if(jekunModelCode != null) {
                //配置车型
                CarConfiguration carConfiguration = carConfigurationRepository.findOne(jekunModelCode);
                if(carConfiguration != null){
                    carLabel = carConfiguration.getCarLabel();
                    carModel = carConfiguration.getCarModel();
                }
            }
        } else {
            //业务凭证扩展表
            VoucherExtension voucherExtension = voucherExtensionRepository.findByVoucherItemId(voucherItem.getId());
            carLicense = voucherExtension.getCarLicense();
        }
        return MapUtil.mapper("carLicense",carLicense,"product",product,
                "productUom",product.getUom(),"qty",voucherItem.getProductQty(),"salesAmount",voucherItem.getPriceTotal(),
                "serveType",product.getServeType(),"dept",voucher.getDept(),"company",voucher.getCompany(),
                "origin",voucher.getId(),"originItem",String.valueOf(voucherItem.getId()),"originBusinessType","voucher",
                "originCreateDate",voucher.getCreateDate(),"source",voucher.getOrigin(),"sourceBusinessType",voucher.getOriginBusinessType(),
                "productGroup",product.getProductGroup(),"classify",product.getClassify(),
                "brand",product.getBrand(),"tax",voucherItem.getTax(),"carLabel",carLabel,"carModel",carModel,
                "pftPriceUnit",voucherItem.getPriceUnit(),"priceUnit",voucherItem.getPriceUnit(),"sign",sign,
                "discountAmount",voucherItem.getDiscount(),"taxCostPrice",
                voucherItem.getTaxCostPrice() * voucherItem.getProductQty(),"couponDiscountAmount",0.00);
    }

    @Override
    public boolean support(String type) {
        return "voucher".equals(type);
    }

}
